import router from './router'
import { getToken } from '@/utils/auth'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
// import store from './store'

const whiteList = ['/login', '/404']

router.beforeEach(async(to, from, next) => {
  // 进度条
  NProgress.start()
  const token = getToken()
  if (token) {
    if (to.path === '/login') {
      next('/')
      NProgress.done()
    } else {
      // await store.dispatch('user/setUsermeuns')
      next()
    }
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
      NProgress.done()
    }
  }
})

router.beforeEach((to, from, next) => {
  document.title = to.meta.title
  next()
})
router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})

